//
//  LWRegisterViewController.m
//  IOSResume
//
//  Created by wynn on 16/4/20.
//  Copyright © 2016年 wynn. All rights reserved.
//

#import "LWRegisterViewController.h"

@interface LWRegisterViewController ()
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UITextField *username;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UITextField *passwordConform;
@property (nonatomic, strong) UIImageView *backGroundImageView;

@end

@implementation LWRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 设置背景图片
    if (!_backGroundImageView) {
        UIImage *backGroundImage = [UIImage imageNamed:@"defaultBackground"];
        self.backGroundImageView = [[UIImageView alloc] initWithImage:backGroundImage];
        self.backGroundImageView.frame = self.view.bounds;
        [self.view addSubview:self.backGroundImageView];
        // 用户名输入框
        self.username = [[UITextField alloc] init];
        self.username.frame = CGRectMake(50.0, 20.0 + 64.0 , SCREEN.width - 100.0 , 40.0);
        self.username.placeholder = @"邮箱";
        [self.view addSubview:self.username];
        self.username.textAlignment = NSTextAlignmentCenter;
        self.username.borderStyle = UITextBorderStyleNone;
        self.username.layer.borderWidth = 1.0;
        self.username.layer.borderColor = [UIColor whiteColor].CGColor;
        self.username.layer.cornerRadius = 20.0;
        self.username.textColor = [UIColor whiteColor];
        [self.username setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        // 密码输入框
        self.password = [[UITextField alloc] init];
        self.password.frame = CGRectMake(50.0, 20.0 + 64.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        self.password.placeholder = @"密码不小于6位";
        [self.view addSubview:self.password];
        self.password.secureTextEntry = YES;
        self.password.textAlignment = NSTextAlignmentCenter;
        self.password.borderStyle = UITextBorderStyleNone;
        self.password.layer.borderWidth = 1.0;
        self.password.layer.borderColor = [UIColor whiteColor].CGColor;
        self.password.layer.cornerRadius = 20.0;
        self.password.textColor = [UIColor whiteColor];
        [self.password setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        // 密码确认输入框
        self.passwordConform = [[UITextField alloc] init];
        self.passwordConform.frame = CGRectMake(50.0, 20.0 + 64.0 + 40.0 + 20.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        self.passwordConform.placeholder = @"昵称";
        [self.view addSubview:self.passwordConform];
        self.passwordConform.secureTextEntry = YES;
        self.passwordConform.textAlignment = NSTextAlignmentCenter;
        self.passwordConform.borderStyle = UITextBorderStyleNone;
        self.passwordConform.layer.borderWidth = 1.0;
        self.passwordConform.layer.borderColor = [UIColor whiteColor].CGColor;
        self.passwordConform.layer.cornerRadius = 20.0;
        self.passwordConform.textColor = [UIColor whiteColor];
        [self.passwordConform setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        // 注册按钮
        self.registerButton = [[UIButton alloc] init];
        self.registerButton.frame = CGRectMake(50.0, 20.0 + 64.0 + 40.0 + 20.0 + 40.0 + 20.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [self.view addSubview:self.registerButton];
        [self.registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.registerButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [self.registerButton addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
        self.registerButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.registerButton setBackgroundColor:APP_COLOR];
        self.registerButton.layer.cornerRadius = 20.0;
        self.registerButton.clipsToBounds = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)actionClick:(UIButton *)button
{
    if (![self.password.text isEqualToString:self.passwordConform.text]) {
        [SVProgressHUD showInfoWithStatus:@"密码不一致"];
        return;
    }
    BmobUser *bUser = [[BmobUser alloc] init];
    bUser.username = self.username.text;
    bUser.password = self.password.text;
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful)
        {
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
        }
        else
        {
            if (error.code == 202) {
                [SVProgressHUD showErrorWithStatus:@"此用户名已存在"];
            }
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
